
############################
#                          # 
# Replication for Table 8  #
#                          #  
############################

emod1 <- errorsarlm(turnout_10 ~ l_stops_10 + black_10 + latino_10 + noncitizen_10 + samehouse_10 + homeowner_10 
                             + l_population_10 + l_median_hh_inc_10 + single_mother_10 + turnout_08, 
                    data = map_data, listw=W_cont_el_mat, zero.policy = T, tol.solve = 1e-12, method = "MC", 
                    na.action = na.omit, etype = "error", control = list(SE_method = "MC"))

emod2 <- errorsarlm(turnout_10 ~ l_search_frisks_10 + black_10 + latino_10 + noncitizen_10 + samehouse_10 + homeowner_10
                             + l_population_10 + l_median_hh_inc_10 + single_mother_10 + turnout_08, 
                    data = map_data, listw=W_cont_el_mat,  zero.policy = T, tol.solve = 1e-12, method = "MC", 
                    na.action = na.omit, etype = "error", control = list(SE_method = "MC"))

emod3 <- errorsarlm(turnout_10 ~ l_force_10 + black_10 + latino_10 + noncitizen_10 + samehouse_10 + homeowner_10
                           + l_population_10 + l_median_hh_inc_10 + single_mother_10 + turnout_08, 
                    data = map_data, listw=W_cont_el_mat,  zero.policy = T, tol.solve = 1e-12, method = "MC", 
                    na.action = na.omit, etype = "error",  control = list(SE_method = "MC"))

emod4 <- errorsarlm(turnout_10 ~ l_furtive_10 + black_10 + latino_10 + noncitizen_10 + samehouse_10 + homeowner_10
                           + l_population_10 + l_median_hh_inc_10 + single_mother_10 + turnout_08, 
                    data = map_data, listw = W_cont_el_mat,  zero.policy = T, tol.solve = 1e-12, method="MC", 
                    na.action = na.omit, etype = "error", control = list(SE_method = "MC"))

emod_list <- list(emod1, emod2, emod3, emod4)

 
error.names <- c("(Intercept)", "Log(Stops)", "Black", "Latino", "Non-citizen", "Same House", "Homeowner", 
                 "Log(Population)", "Log(Income)", "Single Mother", "Turnout 08")

error.2010 <- lapply(emod_list, change.names, error.names)


stargazer(error.2010, title = "Spatial Error Model: Effect of Stops on 2010 Turnout",
           column.labels=c("All Stops","Frisks","Force"), dep.var.caption=c(""), omit.stat=c("f","sigma2","wald"),
           dep.var.labels.include=F,model.numbers=F,font.size="scriptsize",star.cutoffs = c(.05,.01,.001))))



############################
#                          # 
# Replication for Table 9  #
#                          #  
############################


emod5 <- errorsarlm(turnout_08 ~ l_stops_08 + black_08 +  latino_08 + noncitizen_08 + samehouse_08 + homeowner_08 
                            + l_population_08 +  l_median_hh_inc_08 + single_mother_08 + turnout_06, 
                    data = map_data, listw=W_cont_el_mat, zero.policy=T, tol.solve=1e-12, method="MC", 
                    na.action = na.omit, etype = "error",  control = list(SE_method = "MC"))

emod6 <- errorsarlm(turnout_08 ~ l_search_frisks_08 + black_08 +  latino_08  +noncitizen_08 + samehouse_08 + homeowner_08
                             + l_population_08 +  l_median_hh_inc_08 + single_mother_08 + turnout_06, 
                    data = map_data, listw=W_cont_el_mat, zero.policy=T, tol.solve=1e-12, method="MC", 
                    na.action = na.omit, etype = "error", control = list(SE_method = "MC"))

emod7 <- errorsarlm(turnout_08 ~ l_force_08 + black_08 +  latino_08 + noncitizen_08 + samehouse_08 + homeowner_08 
                            + l_population_08 +  l_median_hh_inc_08 + single_mother_08 + turnout_06, 
                    data = map_data, listw=W_cont_el_mat, zero.policy=T, tol.solve=1e-12, method="MC", 
                    na.action = na.omit, etype = "error",  control = list(SE_method = "MC"))

emod8 <- errorsarlm(turnout_08 ~ l_furtive_08 + black_08 +  latino_08  +noncitizen_08 + samehouse_08 + homeowner_08 
                            + l_population_08 +  l_median_hh_inc_08 + single_mother_08 + turnout_06, 
                    data = map_data, listw=W_cont_el_mat,  zero.policy=T, tol.solve=1e-12, method="MC", 
                    na.action = na.omit, etype = "error", control = list(SE_method = "MC"))

error.08.list <- list(emod5, emod6, emod7, emod8)


error.names <- c("(Intercept)", "Log(Stops)", "Black", "Latino", "Non-citizen", "Same House", "Homeowner", 
                 "Log(Population)", "Log(Income)", "Single Mother", "Turnout 06")

error.2008 <- lapply(error.08.list, change.names, error.names)

stargazer(error.2008, title = "Spatial Error Model: Effect of Stops on 2008 Turnout",
           column.labels=c("All Stops","Frisks","Force"), dep.var.caption=c(""), omit.stat=c("f","sigma2","wald"),
           dep.var.labels.include=F,model.numbers=F,font.size="scriptsize",star.cutoffs = c(.05,.01,.001))




############################
#                          # 
# Replication for Table 10 #
#                          #  
############################



emod9 <- errorsarlm(turnout_06 ~ l_stops_06 + black_06 + latino_06 + noncitizen_06 + samehouse_06 + homeowner_06 
                            + l_population_06 + l_median_hh_inc_06 + single_mother_06,
                    data = map_data, listw = W_cont_el_mat,  zero.policy=T, tol.solve=1e-12, method="MC",
                    na.action = na.omit, etype = "error",  control = list(SE_method = "MC"))

emod10 <- errorsarlm(turnout_06 ~ l_search_frisks_06 + black_06 + latino_06 + noncitizen_06 + samehouse_06 + homeowner_06 
                             + l_population_06 + l_median_hh_inc_06 + single_mother_06, 
                     data = map_data, listw = W_cont_el_mat, zero.policy=T, tol.solve=1e-12, method="MC",
                     na.action = na.omit, etype = "error", control = list(SE_method = "MC"))

emod11 <- errorsarlm(turnout_06 ~ l_force_06 + black_06 + latino_06 + noncitizen_06 + samehouse_06 + homeowner_06 
                            + l_population_06 + l_median_hh_inc_06 + single_mother_06, 
                     data = map_data, listw = W_cont_el_mat, zero.policy=T, tol.solve=1e-12, method="MC",
                     na.action = na.omit, etype = "error",  control = list(SE_method = "MC"))

emod12 <- errorsarlm(turnout_06 ~ l_furtive_06 + black_06 + latino_06 + noncitizen_06 + samehouse_06 + homeowner_06 
                            + l_population_06 + l_median_hh_inc_06 + single_mother_06,
                     data = map_data, listw=W_cont_el_mat, zero.policy=T, tol.solve=1e-12, method="MC", 
                     na.action = na.omit, etype = "error", control = list(SE_method = "MC"))

error.06.list <- list(emod9, emod10, emod11, emod12)

error.names <- c("(Intercept)", "Log(Stops)", "Black", "Hispanic", "Non-citizen", "Same House", "Homeowner", 
                 "Log(Population)", "Log(Income)", "Single Mother")


error.2006 <- lapply(error.06.list, change.names, error.names)

stargazer(error.2006, title = "Spatial Error Model: Effect of Stops on 2006 Turnout",
           column.labels=c("All Stops","Frisks","Force", "Furtive"), dep.var.caption=c(""), omit.stat=c("f","sigma2","wald"),
           dep.var.labels.include=F,model.numbers=F,font.size="scriptsize", star.cutoffs = c(.05,.01,.001))

############################
#                          # 
# Replication for Table 11 #
#                          #  
############################


inter10b <- glm( turnout_10 ~ l_stops_10 * black_10 + latino_10 + noncitizen_10 + samehouse_10 + homeowner_10 + 
                      l_population_10 + l_median_hh_inc_10 + single_mother_10  + scale_08,
                 data=data[data$POP100 > 100, ], family = quasibinomial, weights = VEP10)

inter08b <- glm( turnout_08 ~ l_stops_08 * black_08 +  latino_08 + noncitizen_08 + samehouse_08 + homeowner_08 + 
                      l_population_08 + l_median_hh_inc_08 + single_mother_08 + scale_06,
                  data=data[data$POP100 > 100, ], family = quasibinomial, weights = VEP08)

inter06b <- glm( turnout_06 ~ l_stops_06  * black_06 + latino_06 + noncitizen_06 + samehouse_06 + homeowner_06 + 
                      l_population_06 + l_median_hh_inc_06 + single_mother_06,
                  data=data[data$POP100 > 100,], family = quasibinomial, weights = VEP06)


interb = list(inter10b, inter08b, inter06b)
inter.models.black.CI = list(exp(confint(inter10b)), exp(confint(inter08b)), exp(confint(inter06b)))

names = c("(Intercept)", "Log(Stops)", "Black", "Hispanic", "Non-citizen", "Same House", "Homeowner", 
                 "Log(Population)", "Log(Income)", "Single Mother", "Past Turnout", "Log(Stops) * Black")

interb[1:2] <- lapply(interb[1:2], change.names, names)
interb[3]   <- lapply(interb[3],   change.names, names[-11])


McF_R2 <- list( c("Pseudo $R^{2}$", round(unlist(lapply(interb, pseudo.R.quasi)),2)))


stargazer(interb, type="latex", title = "Marginal Effect of \\% Black on Turnout", style ="apsr", 
          dep.var.labels.include =FALSE, digits=2, model.numbers=FALSE, 
          font.size="small", apply.coef=exp, ci.custom= inter.models.black.CI, ci = T, p.auto=F,
          star.cutoffs = c(.05,.01,.001),  add.lines = McF_R2, 
          column.labels= c("\\textbf{2010}", "\\textbf{2008}", "\\textbf{2006}"))

inter13mpb <- glm( turnout_primary ~ l_stops * black + latino + samehouse + homeowner + 
                            l_population + l_median_hh_inc + single_mother + share_dem + turnout_dmp09,
               data = data13[data13$pop_10 > 100, ],  family= quasibinomial, weights = VEP13)

inter13mgb <- glm(turnout_general ~ l_stops * black + latino + samehouse + homeowner + 
                            l_population + l_median_hh_inc + single_mother + share_dem + turnout_mg09 , 
                       data = data13[data13$pop_10 > 100, ], family= quasibinomial, weights = VEP13)

interb2 <- list(inter13mpb, inter13mgb)
interb2.CI <- list(exp(confint(inter13mpb)), exp(confint(inter13mgb)))


McF_R2 <- list( c("Pseudo $R^{2}$", round(unlist(lapply(interb2, pseudo.R.quasi)),2)))

format.stargazer.d( capture.output(stargazer(interb2, 
                    type="latex", title = "Marginal Effect of \\% Black on Turnout", style ="apsr", 
                    dep.var.labels.include =FALSE, digits=2, model.numbers=FALSE, 
                    font.size="small", apply.coef=exp, ci.custom= interb2.CI, ci = T, p.auto=F,
                    star.cutoffs = c(.05,.01,.001),  add.lines = McF_R2, 
                    column.labels= c("\\textbf{2010}", "\\textbf{2008}", "\\textbf{2006}"))) )

############################
#                          # 
# Replication for Table 12 #
#                          #  
############################

inter10h <- glm( turnout_10 ~ l_stops_10  * latino_10 + black_10 + noncitizen_10 
                  + samehouse_10 + homeowner_10 + l_population_10  + l_median_hh_inc_10 + single_mother_10  + scale_08,
                  data=data[data$POP100 > 100, ], family = quasibinomial, weights = VEP10)

inter08h <- glm(turnout_08~ l_stops_08  * latino_08 + black_08 + noncitizen_08
                  + samehouse_08 + homeowner_08 + l_population_08 +  l_median_hh_inc_08 + single_mother_08 + scale_06,
                  data=data[data$POP100 > 100, ], family = quasibinomial, weights = VEP08)

inter06h <- glm(turnout_06 ~ l_stops_06   * latino_06 + black_06 + noncitizen_06
                  + samehouse_06 + homeowner_06 + l_population_06 + l_median_hh_inc_06 + single_mother_06,
                  data=data[data$POP100 > 100,], family = quasibinomial, weights = VEP06)

interh = list(inter10h, inter08h, inter06h)
inter.models.h.CI = list(exp(confint(inter10h)), exp(confint(inter08h)), exp(confint(inter06h)))

names = c("(Intercept)", "Log(Stops)", "Latino", "Black", "Non-citizen", "Same House", "Homeowner", 
                 "Log(Population)", "Log(Income)", "Single Mother", "Past Turnout", "Log(Stops) * Latino")

interh[1:2] = lapply(interh[1:2], change.names, names)
interh[3]   = lapply(interh[3],   change.names, names[-11])

McF_R2 = list( c("Pseudo $R^{2}$", round(unlist(lapply(interh, pseudo.R.quasi)),2)))

stargazer(interh, type="latex", title = "Marginal Effects of \\% Latino on Turnout", style ="apsr", 
          dep.var.labels.include =FALSE, digits=2, model.numbers=FALSE, 
          font.size="small", apply.coef=exp, ci.custom= inter.models.h.CI, ci = T, p.auto=F,
          star.cutoffs = c(.05,.01,.001),  add.lines = McF_R2, 
          column.labels= c("\\textbf{2010}", "\\textbf{2008}", "\\textbf{2006}"))

# Results from these remaining 2 models were manually added to latex file


inter13mph <- glm( turnout_primary ~ l_stops * latino +  black + samehouse + homeowner + 
                            l_population + l_median_hh_inc + single_mother + share_dem + turnout_dmp09,
               data = data13[data13$pop_10 > 100, ], family= quasibinomial, weights = VEP13)

inter13mgh <- glm(turnout_general ~ l_stops * latino + black + samehouse + homeowner + 
                            l_population + l_median_hh_inc + single_mother + share_dem + turnout_mg09 , 
                       data = data13[data13$pop_10 > 100, ], family= quasibinomial, weights = VEP13)

interh2 <- list(inter13mph, inter13mgh)
interh.CI <- list(exp(confint(inter13mph)), exp(confint(inter13mgh)))


McF_R2 <- list( c("Pseudo $R^{2}$", round(unlist(lapply(interh2, pseudo.R.quasi)),2)))


stargazer(interh2, type="latex", title = "Marginal Effect of \\% Latino on Turnout", style ="apsr", 
          dep.var.labels.include =FALSE, digits=2, model.numbers=FALSE, 
          font.size="small", apply.coef=exp, ci.custom= interh.CI, ci = T, p.auto=F,
          star.cutoffs = c(.05,.01,.001),  add.lines = McF_R2, 
          column.labels= c("\\textbf{Primary}", "\\textbf{General}"))

############################
#                          # 
# Replication for Table 13 #
#                          #  
############################


thompson.b.2 <- glm(thompson13~ l_stops * black + latino + samehouse + homeowner 
                        + l_population + l_median_hh_inc + single_mother+  turnout_dmp09 + thompson_09, 
                   data = data13[data13$pop_10 > 100, ], family= quasibinomial, weights = vote_13dmp)

deblasio.b.2 <- glm( deblasio13 ~ l_stops * black + latino + samehouse + homeowner 
                        + l_population + l_median_hh_inc + single_mother + turnout_dmp09 + deblasio_09, 
                   data = data13[data13$pop_10 > 100, ], family= quasibinomial, weights = vote_13dmp)

liu.b.2 <- glm( liu13 ~ l_stops * black + latino + samehouse + homeowner 
                        + l_population + l_median_hh_inc + single_mother + turnout_dmp09 + liu_09, 
                   data = data13[data13$pop_10 > 100, ], family= quasibinomial, weights = vote_13dmp)

primary_b_interact <- list(thompson.b.2, deblasio.b.2, liu.b.2)
primary_b_interact_CI <- list(exp(confint(thompson.b.2)), exp(confint(deblasio.b.2)), exp(confint(liu.b.2)))


names <- c("(Intercept)", "Log(Stops)", "Black", "Latino", "Same House", "Owner Occupied", "Log(Population)",
           "Log(Income)", "Single Mother", "Democratic Turnout '09",  "Past Candidate Support", "Log(Stops) * Black")

primary_b_interact <- lapply(primary_b_interact, change.names, names)

McF_R2 <- list( c("Pseudo $R^{2}$", round(unlist(lapply(primary_b_interact, pseudo.R.quasi)),2)))


stargazer(primary_b_interact, type="latex", title = "Marginal Effects of \\% Black on Candidate Support", style ="apsr", 
          dep.var.labels.include =FALSE, digits=2, model.numbers=FALSE, 
          font.size="small", apply.coef=exp, ci.custom= primary_b_interact_CI, ci = T, p.auto=F,
          star.cutoffs = c(.05,.01,.001),  add.lines = McF_R2, 
          column.labels= c("\\textbf{Thompson}", "\\textbf{De Blasio}", "\\textbf{Liu}"))


############################
#                          # 
# Replication for Table 14 #
#                          #  
############################



thompson.h.2 <- glm( thompson13~ l_stops *latino + black +  samehouse + homeowner 
                        + l_population + l_median_hh_inc + single_mother+  turnout_dmp09 + thompson_09, 
                   data = data13[data13$pop_10 > 100, ], family= quasibinomial, weights = vote_13dmp)

deblasio.h.2 <- glm(deblasio13 ~ l_stops * latino + black +  samehouse + homeowner 
                        + l_population + l_median_hh_inc + single_mother + turnout_dmp09+ deblasio_09, 
                   data = data13[data13$pop_10 > 100, ], family= quasibinomial, weights = vote_13dmp)

liu.h.2      <- glm(liu13 ~ l_stops * latino + black  + samehouse + homeowner 
                        + l_population + l_median_hh_inc + single_mother + turnout_dmp09+ liu_09, 
                   data = data13[data13$pop_10 > 100, ], family= quasibinomial, weights = vote_13dmp)

primary_h_interact <- list(thompson.h.2, deblasio.h.2, liu.h.2)
primary_h_interact_CI <- list(exp(confint(thompson.h.2)), exp(confint(deblasio.h.2)), exp(confint(liu.h.2)))


names <- c("(Intercept)", "Log(Stops)", "Latino", "Black", "Same House", "Homeowner", "Log(Population)",
           "Log(Income)", "Single Mother", "Democratic Turnout '09",  "Past Candidate Support", "Log(Stops) * Latino")

primary_h_interact <- lapply(primary_h_interact, change.names, names)

McF_R2 <- list( c("Pseudo $R^{2}$", round(unlist(lapply(primary_h_interact, pseudo.R.quasi)),2)))


stargazer(primary_h_interact, 
          type="latex", title = "Marginal Effects of \\% Latino on Candidate Support", style ="apsr", 
          dep.var.labels.include =FALSE, digits=2, model.numbers=FALSE, 
          font.size="small", apply.coef=exp, ci.custom= primary_h_interact_CI, ci = T, p.auto=F,
          star.cutoffs = c(.05,.01,.001),  add.lines = McF_R2, 
          column.labels= c("\\textbf{Thompson}", "\\textbf{De Blasio}", "\\textbf{Liu}"))


############################
#                          # 
# Replication for Table 15 #
#                          #  
############################




model1_sq <- glm(turnout_10 ~ l_stops_10 + I(l_stops_10^2) + black_10 + latino_10 + noncitizen_10 
                  + samehouse_10 + homeowner_10 + l_population_10  + l_median_hh_inc_10 + single_mother_10  + scale_08,
                  data=data[data$POP100 > 100, ], family = quasibinomial, weights =VEP10)
           
model2_sq <- glm(turnout_10~ l_search_frisks_10 + I(l_search_frisks_10^2) + black_10 + latino_10 + noncitizen_10
                  + samehouse_10 + homeowner_10 + l_population_10 + l_median_hh_inc_10 + single_mother_10  + scale_08,
                  data=data[data$POP100 > 100, ], family = quasibinomial, weights =VEP10)

model3_sq <- glm(turnout_10 ~ l_force_10 + I(l_force_10^2) + black_10 + latino_10 + noncitizen_10
                  + samehouse_10 + homeowner_10 + l_population_10 +  l_median_hh_inc_10 + single_mother_10 + scale_08,
                  data=data[data$POP100 > 100, ], family = quasibinomial, weights =VEP10)

model4_sq <- glm(turnout_10~ l_furtive_10 + I(l_furtive_10^2) + black_10 + latino_10 + noncitizen_10
                  + samehouse_10 + homeowner_10 + l_population_10 + l_median_hh_inc_10 + single_mother_10 + scale_08,
                  data=data[data$POP100 > 100, ], family = quasibinomial, weights =VEP10)

labels <- c("(Intercept)", "Stops",  "Stops Squared", "Black", "Latino", "Non-citizen", "Same House", "Homeowner", 
           "Log(Population)", "Log(Income)", "Single Mother", "Turnout 08")

New_York_2010_Models <- list(model1_sq, model2_sq, model3_sq, model4_sq)
New_York_2010_Models <- lapply(New_York_2010_Models, change.names, labels)


New_York_2010_CI = list(exp(confint(model1_sq)), exp(confint(model2_sq)), exp(confint(model3_sq)), exp(confint(model4_sq)))

McF_R2 = list( c("Pseudo $R^{2}$", round(unlist(lapply(New_York_2010_Models, pseudo.R.quasi)),2)))


stargazer(New_York_2010_Models,  type="latex", title = "Effect of Police Stops on Voter Turnout: 2010, with Squared Term", style ="apsr", 
          dep.var.labels.include =FALSE, digits=2, model.numbers=FALSE, 
          font.size="small", apply.coef=exp, ci.custom= New_York_2010_CI, ci = T, p.auto=F,
          star.cutoffs = c(.05,.01,.001),  add.lines = McF_R2,
          column.labels= c("\\textbf{Stops}", "\\textbf{Frisks and Searches}", "\\textbf{Force}", 
                           "\\textbf{Furtive Movements}"))


############################
#                          # 
# Replication for Table 16 #
#                          #  
############################



model5_sq <- glm(turnout_08  ~ l_stops_08 + I(l_stops_08^2) + black_08 +  latino_08 + noncitizen_08
                  + samehouse_08 + homeowner_08 + l_population_08 +  l_median_hh_inc_08 + single_mother_08 + scale_06,
                  data=data[data$POP100 > 100, ], family = quasibinomial, weights = VEP08)

model6_sq <- glm(turnout_08 ~ l_search_frisks_08 + I(l_search_frisks_08^2) + black_08 +  latino_08 +  noncitizen_08
                  + samehouse_08 + homeowner_08 + l_population_08 +  l_median_hh_inc_08 + single_mother_08 + scale_06,
                  data=data[data$POP100 > 100, ], family = quasibinomial, weights = VEP06)

model7_sq <- glm(turnout_08 ~ l_force_08  + I(l_force_08^2) + black_08 +  latino_08 +  noncitizen_08
                  + samehouse_08 + homeowner_08 + l_population_08 +  l_median_hh_inc_08 + single_mother_08 + scale_06,
                  data=data[data$POP100 > 100, ], family = quasibinomial, weights = VEP08)
                  
model8_sq <- glm( turnout_08 ~ l_furtive_08 + I(l_furtive_08^2) + black_08 +  latino_08 +  noncitizen_08
                  + samehouse_08 + homeowner_08 + l_population_08 +  l_median_hh_inc_08 + single_mother_08 + scale_06),
                  data=data[data$POP100 > 100, ], family = quasibinomial, weights = VEP08)

labels <- c("(Intercept)", "Log(Stops)",  "Stops Squared", "Black", "Latino", "Non Citizen", "Same House", "Homeowner", 
           "Log(Population)", "Log(Income)", "Single Mother", "Turnout 06")

New_York_2008_Models <- list(model5_sq, model6_sq, model7_sq, model8_sq)
New_York_2008_Models <- lapply(New_York_2008_Models, change.names, labels)

New_York_2008_CI = list(exp(confint(model5_sq)), exp(confint(model6_sq)), exp(confint(model7_sq)), exp(confint(model8_sq)))


McF_R2 = list( c("Pseudo $R^{2}$", round(unlist(lapply(New_York_2008_Models, pseudo.R.quasi)),2)))


stargazer(New_York_2008_Models,  type="latex", title = "Effect of Police Stops on Voter Turnout: 2008, with Squared Term", style ="apsr", 
                    dep.var.labels.include =FALSE, digits=2, model.numbers=FALSE, 
                    font.size="small", apply.coef=exp, ci.custom= New_York_2008_CI, ci = T, p.auto=F,
                    star.cutoffs = c(.05,.01,.001),  add.lines = McF_R2, 
                    column.labels= c("\\textbf{Stops}", "\\textbf{Frisks and Searches}", "\\textbf{Force}", "\\textbf{Furtive Movements}"))


############################
#                          # 
# Replication for Table 17 #
#                          #  
############################


model9_sq <- glm(turnout_06~ l_stops_06  + I(l_stops_06^2) + black_06 + latino_06 + noncitizen_06
                  + samehouse_06 + homeowner_06 + l_population_06 + l_median_hh_inc_06 + single_mother_06,
                  data=data[data$POP100 > 100,], family = quasibinomial, weights = VEP06)

model10_sq <- glm(turnout_06 ~ l_search_frisks_06 + I(l_search_frisks_06^2) + black_06+ latino_06 + noncitizen_06
                  + samehouse_06 + homeowner_06 + l_population_06 + l_median_hh_inc_06 + single_mother_06,
                  data=data[data$POP100 > 100,], family = quasibinomial, weights = VEP06)

model11_sq <- glm(turnout_06~ l_force_06 + I(l_force_06^2) + black_06 + latino_06 + noncitizen_06
                  + samehouse_06 + homeowner_06 + l_population_06 + l_median_hh_inc_06 + single_mother_06,
                  data = data[data$POP100 > 100, ], family = quasibinomial, weights = VEP06)

model12_sq <- glm(turnout_06 ~ l_furtive_06 + I(l_furtive_06^2) + black_06 + latino_06 + noncitizen_06
                  + samehouse_06 + homeowner_06 + l_population_06 +  l_median_hh_inc_06 + single_mother_06,
                  data = data[data$POP100 > 100, ], family = quasibinomial, weights = VEP06)

labels <- c("(Intercept)", "Stops",  "Stops^2", "Black", "Latino", "Non Citizen", "Same House", "Homeowner", 
           "Log(Population)", "Log(Income)", "Single Mother")

New_York_2006_Models <- list(model9_sq, model10_sq, model11_sq, model12_sq)
New_York_2006_Models <- lapply(New_York_2006_Models, change.names, labels)

New_York_2006_CI = list(exp(confint(model9_sq)), exp(confint(model10_sq)), exp(confint(model11_sq)), exp(confint(model12_sq)))

McF_R2 = list( c("Pseudo $R^{2}$", round(unlist(lapply(New_York_2006_Models, pseudo.R.quasi)),2)))



stargazer(New_York_2006_Models, type="latex", title = "2006 Binomial Regression (Per Capita)", style ="apsr", 
          dep.var.labels.include =FALSE, digits=2, model.numbers=FALSE, 
          font.size="small", apply.coef=exp, ci.custom= New_York_2006_CI, ci = T, p.auto=F,
          star.cutoffs = c(.05,.01,.001),  add.lines = McF_R2, 
          column.labels= c("\\textbf{Stops}", "\\textbf{Frisks and Searches}", "\\textbf{Force}", "\\textbf{Furtive}"))




############################
#                          # 
# Replication for Table 18 #
#                          #  
############################



primary_sq <- glm( turnout_primary ~ l_stops + I(l_stops^2) + black + latino + samehouse + homeowner + 
                            l_population + l_median_hh_inc + single_mother + share_dem + turnout_dmp09, 
               data = data13[data13$pop_10 > 100,],  family= quasibinomial, weights = VEP13)

mayoral.general_sq <- glm( turnout_general  ~ l_stops + I(l_stops^2) + black + latino + samehouse + homeowner + 
                            l_population + l_median_hh_inc + single_mother + share_dem + turnout_mg09 , 
                       data = data13[data13$pop_10 > 100,], family= quasibinomial, weights = VEP13)


labels <- c("(Intercept)", "Stops",  "Stops^2", "Black", "Latino", "Same House", "Homeowner", 
           "Log(Population)", "Log(Income)", "Single Mother", "Democratic Share", "Turnout `09")

mayoral_models_sq = list(primary_sq, mayoral.general_sq)
mayoral_models_sq_CI = list(exp(confint(primary_sq)), exp(confint(mayoral.general_sq)))

McF_R2 = list( c("Pseudo $R^{2}$", round(unlist(lapply(mayoral_models_sq, pseudo.R.quasi)),2)))

mayoral_models_sq <- lapply(mayoral_models_sq, change.names, labels)


stargazer(mayoral_models_sq, type="latex", title = "Some Title", style ="apsr", 
          dep.var.labels.include =FALSE, digits=2, model.numbers=FALSE, 
          font.size="small", apply.coef=exp, ci.custom= mayoral_models_sq_CI, ci = T, p.auto=F,
          star.cutoffs = c(.05,.01,.001),  add.lines = McF_R2,
          column.labels= c("\\textbf{Primary  Election}", "\\textbf{Mayoral Election}"))
 



############################
#                          # 
# Replication for Table 19 #
#                          #  
############################


thompson_sq <- glm( thompson13 ~ l_stops  + I(l_stops^2) + black + latino + samehouse + homeowner +
                l_population + l_median_hh_inc  + single_mother + turnout_dmp09 + thompson_09, 
                data = data13[data13$pop_10 > 100, ], family= quasibinomial, weights =vote_13dmp)

deblasio_sq <- glm(deblasio13~ l_stops + I(l_stops^2) + black + latino + samehouse + homeowner +
                     l_population  + l_median_hh_inc + single_mother + turnout_dmp09 + deblasio_09,
                data = data13[data13$pop_10 > 100, ], family= quasibinomial, weights =vote_13dmp)

liu_sq <- glm( liu13 ~ l_stops + I(l_stops^2) + black + latino + samehouse + homeowner +
                l_population + l_median_hh_inc+ single_mother + turnout_dmp09 + liu_09,
           data = data13[data13$pop_10 > 100, ], family= quasibinomial, weights =vote_13dmp)

candidate_suport_sq <- list(thompson_sq, deblasio_sq, liu_sq)

candidate_suport_sq_CI = list(exp(confint(thompson_sq)), exp(confint(deblasio_sq)), exp(confint(liu_sq)))

McF_R2 = list( c("Pseudo $R^{2}$", round(unlist(lapply(candidate_suport_sq, pseudo.R.quasi)),2)))

labels <- c("Log(Stops)", "Log(Stops)^2", "Black", "Latino", "Same House", "Homeowner", "Log(Population)", "Log(Income)", 
            "Single Mother", "Turnout 09: Primary", "Thompson `09", "De Blasio `09", "Liu `09","Constant")

stargazer(candidate_suport_sq, type="latex", title = "Effect of Police Stops on Candidate Choice: 2013 Democratic Primary, with Squared Term", 
          style ="apsr", dep.var.labels.include =FALSE, digits=2, model.numbers=FALSE, 
          font.size="small", apply.coef=exp, ci.custom= candidate_suport_sq_CI, ci = T, p.auto=F,
          star.cutoffs = c(.05,.01,.001),  add.lines = McF_R2, covariate.labels = labels,
          column.labels= c("\\textbf{Thompson}", "\\textbf{de Blasio}", "\\textbf{Liu}"))



